package com.example.service.impl;

import ch.qos.logback.classic.Logger;
import com.example.entity.EmergencyRescue;
import com.example.ext.entity.ExtEmergencyRescue;
import com.example.mapp.ElevatorMapp;
import com.example.mapp.EmergencyRescueMapp;
import com.example.service.EmergencyRescueService;
import com.example.util.ResultUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by youkun on 2017/10/25.
 * 救援表
 */
@Service
public class EmergencyRescueServiceImpl implements EmergencyRescueService {

    private final static Logger logger = (Logger) LoggerFactory.getLogger(EmergencyRescueServiceImpl.class);

    @Autowired
    private EmergencyRescueMapp emergencyRescueMapp;

//	@Autowired
//	private PromotionMapp promotionMapp;
	
	@Autowired
	private ElevatorMapp elevatorMapp;

    /**
     * 查询电梯救援列表
     * @param extEmergencyRescue
     * @return
     */
    @Override
    public Map<String, Object> queryList(ExtEmergencyRescue extEmergencyRescue) {
        List<ExtEmergencyRescue> list =null;
        Map<String,Object> map = new HashMap<String,Object>();
        try {
            Page<ExtEmergencyRescue> page = PageHelper.startPage(extEmergencyRescue.getCurrentPage(), extEmergencyRescue.getPageSize());
            list = emergencyRescueMapp.findList(extEmergencyRescue);
            map.put("list",list);
            map.put("total",page.getTotal());

        } catch (Exception e) {
            logger.error(e.getMessage());
            return ResultUtil.returnError("查询失败"+e.getMessage(),null);
        }
        return ResultUtil.returnSuccess("查询成功",map);
    }

    /**
     * 查询电梯救援详情
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> queryById(String id) {
        ExtEmergencyRescue emergencyRescue = null;
        try {
            emergencyRescue = emergencyRescueMapp.findById(id);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return ResultUtil.returnError("查询失败"+e.getMessage(),null);
        }
        return ResultUtil.returnSuccess("查询成功",emergencyRescue);
    }

    @Override
    @Transactional
    public Map<String, Object> add(EmergencyRescue emergencyRescue) {
        try {
            emergencyRescueMapp.add(emergencyRescue);
//			Elevator elevator = elevatorMapp.queryById(String.valueOf(emergencyRescue.getElevatorId()));
//			if (null != elevator && null != elevator.getSafeUserId()) {
//				Map<String, Object> map1 = new HashMap<>();
//				map1.put("type", "3");
//				map1.put("userId", elevator.getSafeUserId());
//				Promotion promotion1 = promotionMapp.findById(map1);
//				if (null != promotion1 && promotion1.getId() != null) {
//					promotion1 = PromotionUtil.calc(promotion1, "3", 20, false);
//					promotionMapp.update(promotion1);
//				} else {
//					promotion1 = new Promotion();
//					promotion1.setType("3");
//					promotion1.setUserId(elevator.getSafeUserId().toString());
//					promotion1 = PromotionUtil.calc(promotion1, "3", 20, true);
//					promotionMapp.insert(promotion1);
//				}
//				PromotionRecord promotionRecord = new PromotionRecord();
//				promotionRecord.setUserId(promotion1.getUserId());
//				promotionRecord.setType(promotion1.getType());
//				promotionRecord.setEmpiricItem("提交一次应急救援记录");
//				promotionRecord.setAddedTime(PromotionUtil.getTime());
//				promotionRecord.setEmpiricValue(20);
//				promotionMapp.insertRecord(promotionRecord);
//				if (null != elevator.getUsedPartyId()) {
//					PromotionUtil.calcParty(promotionMapp, 20, "2", elevator.getUsedPartyId());
//					PromotionRecord promotionRecord1 = new PromotionRecord();
//					promotionRecord1.setUserId(String.valueOf(elevator.getUsedPartyId()));
//					promotionRecord1.setType("2");
//					promotionRecord1.setEmpiricItem("提交一次应急救援记录");
//					promotionRecord1.setAddedTime(PromotionUtil.getTime());
//					promotionRecord1.setEmpiricValue(20);
//					promotionMapp.insertRecord(promotionRecord1);
//				}
//			}
			//要求不积分维保人员， 只积分 使用单位 2018-10-13
//			if (null != elevator && elevator.getMaintenanceUserFirst() != null) {
//				Map<String, Object> map2 = new HashMap<>();
//				map2.put("type", "4");
//				map2.put("userId", elevator.getMaintenanceUserFirst());
//				Promotion promotion2 = promotionMapp.findById(map2);
//				if (null != promotion2 && promotion2.getId() != null) {
//					promotion2 = PromotionUtil.calc(promotion2, "4", 20, false);
//					promotionMapp.update(promotion2);
//				} else {
//					promotion2 = new Promotion();
//					promotion2.setType("4");
//					promotion2.setUserId(elevator.getMaintenanceUserFirst().toString());
//					promotion2 = PromotionUtil.calc(promotion2, "4", 20, true);
//					promotionMapp.insert(promotion2);
//				}
//				PromotionRecord promotionRecord = new PromotionRecord();
//				promotionRecord.setUserId(promotion2.getUserId());
//				promotionRecord.setType(promotion2.getType());
//				promotionRecord.setEmpiricItem("提交一次应急救援记录");
//				promotionRecord.setAddedTime(PromotionUtil.getTime());
//				promotionRecord.setEmpiricValue(20);
//				promotionMapp.insertRecord(promotionRecord);
//				if (null != elevator.getMaintenancePartyId()) {
//					PromotionUtil.calcParty(promotionMapp, 20, "1", elevator.getMaintenancePartyId());
//					PromotionRecord promotionRecord1 = new PromotionRecord();
//					promotionRecord1.setUserId(String.valueOf(elevator.getMaintenancePartyId()));
//					promotionRecord1.setType("1");
//					promotionRecord1.setEmpiricItem("提交一次应急救援记录");
//					promotionRecord1.setAddedTime(PromotionUtil.getTime());
//					promotionRecord1.setEmpiricValue(20);
//					promotionMapp.insertRecord(promotionRecord1);
//
//				}
//			}
//			if (null != elevator && elevator.getMaintenanceUserSecond() != null) {
//				Map<String, Object> map2 = new HashMap<>();
//				map2.put("type", "4");
//				map2.put("userId", elevator.getMaintenanceUserSecond());
//				Promotion promotion2 = promotionMapp.findById(map2);
//				if (null != promotion2 && promotion2.getId() != null) {
//					promotion2 = PromotionUtil.calc(promotion2, "4", 20, false);
//					promotionMapp.update(promotion2);
//				} else {
//					promotion2 = new Promotion();
//					promotion2.setType("4");
//					promotion2.setUserId(elevator.getMaintenanceUserSecond().toString());
//					promotion2 = PromotionUtil.calc(promotion2, "4", 20, true);
//					promotionMapp.insert(promotion2);
//				}
//				PromotionRecord promotionRecord = new PromotionRecord();
//				promotionRecord.setUserId(promotion2.getUserId());
//				promotionRecord.setType(promotion2.getType());
//				promotionRecord.setEmpiricItem("提交一次应急救援记录");
//				promotionRecord.setAddedTime(PromotionUtil.getTime());
//				promotionRecord.setEmpiricValue(20);
//				promotionMapp.insertRecord(promotionRecord);
//				if (null != elevator &&  null != elevator.getMaintenancePartyId()) {
//					PromotionUtil.calcParty(promotionMapp, 20, "1", elevator.getMaintenancePartyId());
//					PromotionRecord promotionRecord1 = new PromotionRecord();
//					promotionRecord1.setUserId(String.valueOf(elevator.getMaintenancePartyId()));
//					promotionRecord1.setType("1");
//					promotionRecord1.setEmpiricItem("提交一次应急救援记录");
//					promotionRecord1.setAddedTime(PromotionUtil.getTime());
//					promotionRecord1.setEmpiricValue(20);
//					promotionMapp.insertRecord(promotionRecord1);
//				}
//			}
//			if (null != emergencyRescue.getReceivedTime() && null != emergencyRescue.getRescueEndTime() && PromotionUtil
//					.calcDate(emergencyRescue.getRescueEndTime(), emergencyRescue.getReceivedTime(), "2") <= 30) {
//				PromotionUtil.calcParty(promotionMapp, 500, "1", elevator.getMaintenancePartyId());
//				PromotionRecord promotionRecord1 = new PromotionRecord();
//				promotionRecord1.setUserId(String.valueOf(elevator.getMaintenancePartyId()));
//				promotionRecord1.setAddedTime(PromotionUtil.getTime());
//				promotionRecord1.setType("1");
//				promotionRecord1.setEmpiricItem("应急救援及时到达（30分钟内）电梯故障现场");
//				promotionRecord1.setEmpiricValue(500);
//				promotionMapp.insertRecord(promotionRecord1);
//			}
        } catch (Exception e) {
        	TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            logger.error(e.getMessage(),e);
            return ResultUtil.returnError("新增失败"+e.getMessage(),null);
        }
        return ResultUtil.returnSuccess("新增成功",null);
    }
}
